package k.daniel.android.listview.expand;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.lidroid.xutils.ViewUtils;

import java.util.List;

import k.daniel.android.util.SimpleLog;

/**
 * Created by daniel on 2015/3/17.
 */
public abstract class BaseExpendAdapter<T extends BaseExpendEntry, GH extends BaseExpendGroupHolder, ChildHolder extends BaseExpendChildHolder> extends BaseExpandableListAdapter {
    protected   Context context;
    protected  List<T> mainDatas;
//    protected   ReflectUtil reflectUtil = new ReflectUtil(getClass());
    protected SimpleLog log = new SimpleLog(this);
    protected BaseExpendAdapter(Context context, List<T> mainDatas) {
        this.context = context;
        this.mainDatas = mainDatas;
    }

    public void setMainDatas(List<T> mainDatas) {
        this.mainDatas = mainDatas;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
//        logO.i("getGroupCount:"+mainDatas.size());
        return mainDatas.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
//        logO.i("getChildrenCount:"+mainDatas.size());
        return mainDatas.get(groupPosition).getChilds().size();
    }
    @Override
    public Object getGroup(int groupPosition) {
        return mainDatas.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mainDatas.get(groupPosition).getChilds().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    public T getGroupItem(int groupPosition){
        return mainDatas.get(groupPosition);
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            log.i("Group:"+groupPosition+"Create View.");
            convertView = View.inflate(context, getGroupLayoutId(groupPosition), null);
            GH holder = getGroupHolder(groupPosition);
            holder.convertView = convertView;
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        }
        GH holder = (GH) convertView.getTag();
        holder.groupPosition = groupPosition;
        processGroupView(holder,isExpanded, groupPosition);
        return convertView;
    }

    protected abstract GH getGroupHolder(int groupPosition);

    public abstract void processGroupView(GH holder, boolean isExpanded, int groupPosition);
    public abstract int getGroupLayoutId(int groupPosition);

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            log.i("Group:"+groupPosition+"-"+"Child:"+childPosition+"Create View.");
            convertView = View.inflate(context, getChildLayoutId(groupPosition, childPosition), null);
            ChildHolder holder = getChildHolder( groupPosition, childPosition);
            holder.convertView = convertView;
            holder.setOnExpItemClickListener(mOnExpItemClickListener);
            holder.setAdapter(this);
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
            convertView.setOnClickListener(holder);
        }
        log.i("Group:"+groupPosition+"-"+"Child:"+childPosition+"Process View.");
        ChildHolder holder = (ChildHolder) convertView.getTag();
        holder.groupPosition = groupPosition;
        holder.childPosition = childPosition;
        holder.setGroup(getGroupItem(groupPosition));
        processChildView(holder, groupPosition, childPosition);
        return convertView;
    }
    protected abstract ChildHolder getChildHolder(int groupPosition, int childPosition);

    protected abstract void processChildView(ChildHolder holder, int groupPosition, int childPosition);
    protected abstract int getChildLayoutId(int groupPosition, int childPosition);
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public OnExpItemClickListener getOnExpItemClickListener() {
        return mOnExpItemClickListener;
    }

    public void setOnExpItemClickListener(OnExpItemClickListener onExpItemClickListener) {
        this.mOnExpItemClickListener = onExpItemClickListener;
    }

    OnExpItemClickListener mOnExpItemClickListener;

    public void removeItem(int groupPosition, int childPosition) {
        mainDatas.get(groupPosition).getChilds().remove(childPosition);
        notifyDataSetChanged();
    }

    public interface OnExpItemClickListener<T> {
        public  void OnItemClick(BaseExpendAdapter adapter, BaseExpendChildHolder baseExpChildHolder, View v);
    }

}
